package cn.agiledata.bank.login.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WebUtil;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.action.IPBBaseActionMapping;
import com.vnv.infrastructure.exception.IPBDuplicateSubmitException;

public class LogoutAction extends IPBBaseAction implements Constant{

    protected String _preProcess(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		IPBBaseActionMapping ipbMapping = (IPBBaseActionMapping) mapping;
		int accessLevel = ipbMapping.getAccessLevel();

		// check for access control
		// if (accessLevel > 0) {
		int permission = hasAccessPermission(accessLevel, request);
		if (permission == 0) {
			if(!StringUtil.isStrEmpty(request.getParameter("from"))) {
				return "success";
			} else {
				return "global_invalid_session";
			}
			
		} else if (permission == -1) {
			
			return "global_denied_access";
		}
		// }

		// check for parameter
		/*
		 * String param = mapping.getParameter(); if (param == null) {
		 * m_logger.error("Mapping definition of [" + mapping.getPath() + "]
		 * does not have a parameter property"); String[] args = {
		 * mapping.getPath() }; throw new
		 * IPBException(ErrorCode.ACTION_DISPATCH_MAPPING_ERROR, args); }
		 */

		// check whether we need to handle duplicate form submissions
		boolean bValidToken = true;
		if (ipbMapping.isValidateToken()) {
			bValidToken = isTokenValid(request, false);
		}
		if (bValidToken == false) {
			// handle the duplicate form submission scenario
			actionForm.reset(mapping, request);
			throw new IPBDuplicateSubmitException(
					"cn.agiledata.infrastructure.action.form.invalid");
		} else {
			return null;
		}
	}


	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward logout(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		AccountSummary user = (AccountSummary)WebUtil.getSessionObject(request,SESSION_ACCOUNT_SUMMARY);
		if(user != null) {
			String message = (String) request.getSession().getAttribute("lockinfo");
			if (StringUtils.isNotEmpty(message)){
				request.setAttribute(MESSAGE, ACCOUNT_LOCKED);
				request.getSession().removeAttribute("lockinfo");
			}
			
			message = (String) request.getSession().getAttribute("payPasswordErrorMsg");
			if (StringUtils.isNotEmpty(message)) {
				request.setAttribute(MESSAGE, message);
				request.getSession().removeAttribute("payPasswordErrorMsg");
			}
			
			request.getSession().invalidate();//触发cn.agiledata.bank.common.web.VNVSessionListener.sessionDestroyed()运行
		}
		
		return mapping.findForward(request.getParameter("from"));
	}

}
